import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/home_business/dark_home_page.dart';
import 'package:xvideos/hj_page/home_business/home_main_page.dart';
import 'package:xvideos/hj_page/main_page/view/video_main_bottom_view.dart';
import 'package:xvideos/hj_page/short_video/short_video_main_page.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/tools_base/widget/keep_alive_widget.dart';

import '../community_business/community_main_page_page/community_main_page.dart';
import '../home_business/widget/home_txt_marquee.dart';
import '../mine_business/home_mine_page.dart';
import 'main_page_logic.dart';

class MainPage extends StatefulWidget {
  static const routeName = '/mainpage';

  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final state = Get.find<MainPageLogic>().state;
  final logic = Get.find<MainPageLogic>();

  DateTime? _lastPressedAt; //上次点击时间

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (bool didPop) async {
        if (didPop) return;
        if (closeOnConfirm()) SystemNavigator.pop();
      },
      child: GetBuilder<MainPageLogic>(
        builder: (_) {
          return Scaffold(
            resizeToAvoidBottomInset: false,
            body: Column(
              children: [
                Expanded(
                  child: PageView(
                    controller: state.pageController,
                    physics: NeverScrollableScrollPhysics(),
                    children: [
                      Stack(
                        fit: StackFit.expand,
                        children: [
                          HomeMainPage(),
                          Positioned(
                            bottom: 0,
                            left: 0,
                            right: 0,
                            child: HomeTxtMarQuee(typeValue: 1),
                          ),
                        ],
                      ),
                      Obx(() => ShortVideoMainPage(
                            index: logic.shortIndex.value,
                          )), //短视频
                      Obx(() => DarkHomePage(defaultId: logic.darkInitTabId.value)), // 暗网
                      Stack(
                        children: [
                          Obx(
                            () => CommunityMainPage(
                              index: logic.communityIndex.value,
                              subTabIndex: logic.communitySubIndex.value,
                            ), //社区
                          ),
                          Positioned(
                            bottom: 0,
                            left: 0,
                            right: 0,
                            child: HomeTxtMarQuee(typeValue: 1),
                          ),
                        ],
                      ),
                      HomeMinePage().keepAlive, //我的
                    ],
                  ),
                ),
                VideoMainBottomView(),
              ],
            ),
          );
        },
      ),
    );
  }

  // 返回键退出
  bool closeOnConfirm() {
    DateTime now = DateTime.now();
    // 物理键，两次间隔大于4秒, 退出请求无效
    if (_lastPressedAt == null || now.difference(_lastPressedAt!) > const Duration(seconds: 2)) {
      _lastPressedAt = now;
      showToast('再次返回即可退出');
      return false;
    }
    // 退出请求有效
    _lastPressedAt = null;
    return true;
  }
}
